/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.control;

import edu.hpvas.persistence.dao.PersonDAO;
import edu.hpvas.persistence.dao.VeterinarianDAO;
import edu.hpvas.persistence.entity.Owner;
import edu.hpvas.persistence.entity.Person;
import edu.hpvas.persistence.entity.Veterinarian;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sony
 */
public class VeterinarianController {
    private VeterinarianDAO vetDao = new VeterinarianDAO();
    private Veterinarian veterinarian = new Veterinarian();
    private PersonDAO personDao= new PersonDAO();
    
    
    
    public Veterinarian getVeterinarian(Person id){
        veterinarian = vetDao.findVeterinarianByIdPerson(id);
        return veterinarian;
    }
    public ArrayList<Veterinarian> searchVeterinarian(String name){
        List<Veterinarian> vetAux = vetDao.findVeterinarianEntities();
        ArrayList<Veterinarian> veterinarians = new ArrayList<>();
        for(Veterinarian v: vetAux){
            if((v.getIdPerson().getName().toLowerCase() + " " + v.getIdPerson().getLastName()
                    .toLowerCase()).contains(name.toLowerCase())){
                veterinarians.add(v);
            }
        }
        return veterinarians;
    }
    public boolean updateVeterinarian(Veterinarian veterinarian, Integer idPerson,Veterinarian newvet ){
        try {
            personDao.edit(veterinarian.getIdPerson());
            vetDao.edit(veterinarian,newvet);
            return true;
        } catch (Exception ex) {
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
    }
    public Veterinarian serchByName(Person person){
        return vetDao.findVeterinarianByIdPerson(person);
    }
    
    public boolean Veterinariancreate(Veterinarian vet,Person p){
        List<Person> persons = personDao.findPersonEntities();
        int id=1;
        for(Person person: persons){
            if(person.getId().intValue()!=id){
                break;
            }
            id++;
        }
        p.setId(id);
        try {
            personDao.create(p);
        } catch (Exception ex) {
            
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        System.out.println("se creo la persona");
        List<Veterinarian> veterinarians = vetDao.findVeterinarianEntities();
        int idV=1;
        for(Veterinarian v: veterinarians){
            if(v.getId().intValue()!=idV){
                break;
            }
            idV++;
        }
        vet.setId(idV);
        vet.setIdPerson(p);
        try {
            vetDao.create(vet);
        } catch (Exception ex) {
            Logger.getLogger(OwnerController.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
}
